﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;

/// <summary>
/// Summary description for Util
/// </summary>
public class Util
{
	public Util()
	{
	}

    /// <summary>
    /// Convert string to Date folowing format
    /// DateFormat: "yyyy/mm/dd" | "mm/dd/yyyy" | "dd/mm/yyyy"
    /// if string is empty then return MinValue
    /// </summary>
    /// <param name="strDate"></param>
    /// <param name="strFormat"></param>
    /// <returns></returns>
    public static DateTime ConvertToDate(string strDate, string strFormat)
    {
        DateTime dResult = DateTime.MinValue;
        string sDate = "";
        string sMonth = "";
        string sYear = "";
        if ((string.IsNullOrEmpty(strDate)))
        {
            return dResult;
        }
        switch (strFormat)
        {
            case "dd/mm/yyyy":
                sDate = strDate.Substring(0, 2);
                sMonth = strDate.Substring(2, 2);
                sYear = strDate.Substring(4, 4);
                dResult = new DateTime(int.Parse(sYear), int.Parse(sMonth), int.Parse(sDate));
                break;

            case "mm/dd/yyyy":
                sMonth = strDate.Substring(0, 2);
                sDate = strDate.Substring(2, 2);
                sYear = strDate.Substring(4, 4);
                dResult = new DateTime(int.Parse(sYear), int.Parse(sMonth), int.Parse(sDate));
                break;

            case "yyyy/mm/dd":
                sYear = strDate.Substring(0, 4);
                sMonth = strDate.Substring(4, 2);
                sDate = strDate.Substring(6, 2);
                dResult = new DateTime(int.Parse(sYear), int.Parse(sMonth), int.Parse(sDate));
                break;

            case "YYYY/MM/DD":
                sYear = strDate.Substring(0, 4);
                sMonth = strDate.Substring(5, 2);
                sDate = strDate.Substring(8, 2);
                dResult = new DateTime(int.Parse(sYear), int.Parse(sMonth), int.Parse(sDate));
                break;
        }
        return dResult;
    }

    /// <summary>
    /// Convert date to string
    /// string have format: "yyyymmdd"
    /// </summary>
    /// <param name="dateValue"></param>
    /// <returns></returns>
    public static string ConvertDateToString(DateTime dateValue)
    {
        if (dateValue != null && dateValue != DateTime.MinValue)
        {
            string sYear = "";
            string sMonth = "";
            string sDay = "";
            switch (dateValue.Year.ToString().Length)
            {
                case 1:
                    sYear = "000" + dateValue.Year.ToString();
                    break;
                case 2:
                    sYear = "00" + dateValue.Year.ToString();
                    break;
                case 3:
                    sYear = "0" + dateValue.Year.ToString();
                    break;
                default:
                    sYear = dateValue.Year.ToString();
                    break;
            }
            switch (dateValue.Month.ToString().Length)
            {
                case 1:
                    sMonth = "0" + dateValue.Month.ToString();
                    break;
                default:
                    sMonth = dateValue.Month.ToString();
                    break;
            }
            switch (dateValue.Day.ToString().Length)
            {
                case 1:
                    sDay = "0" + dateValue.Day.ToString();
                    break;
                default:
                    sDay = dateValue.Day.ToString();
                    break;
            }
            return sYear + sMonth + sDay;
        }
        else { return ""; }
    }

    public static bool SendEmail(string from, string to, string subject, string body)
    {
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true;

        MailAddress fromSender = new MailAddress(from, "VIETNAM TRAVEL VST Co.,Ltd");
        MailAddress toSender = new MailAddress(to, "Your recepient name");
        MailMessage message = new MailMessage(fromSender, toSender);
        message.Body = body;
        message.Subject = subject;

        NetworkCredential myCreds = new NetworkCredential("vietnam.tour.201107@gmail.com", "thang123", "");
        client.Credentials = myCreds;

        try
        {
            client.Send(message);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

}